<?php

namespace Blog\Model;

use Common\Model\BaseModel;

class ArticleModel extends BaseModel
{
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME),
        ['content', 'htmlspecialchars_decode', 3, 'function'],
    );
    protected $_validate = [
        ['title', 'require', '文章标题不能为空'],
        ['content', 'require', '文章内容不能为空'],
        ['category_id', 'require', '文章分类不能为空'],
        ['author', 'require', '文章作者不能为空'],
    ];
    protected $delete_list = false;
    protected $num2text = ['status' => [1 => '公开', -1 => '草稿', -2 => '私密'],];

    public function get_date_st($limit = 12, $format = '%Y年%m月')
    {
        $st = $this->where(['status' => ['egt', 1]])->field('date_format(post_date,"' . $format . '") st_date, count(id) nums')->group('date_format(post_date, "' . $format . '")')->limit($limit)->select();
        return $st;
    }

    public function get_normal_article($page = 10)
    {
        if(is_array($this->options['where'])) {
            $this->options['where'] = array_merge(['status'=>['egt',1]],$this->options['where']);
        }
        $list = $this->findPage();
        return $list;
    }
    public function normal_map($map) {
        return array_merge(['status'=>['egt', 1]], $map);
    }

    public function _after_find(&$result, $options)
    {
        parent::_after_find($result, $options);
        if ($result['id']) {
            $result['comments'] = M('ArticleReply')->where(['post_id' => $result['id']])->count();
            $result['like'] = 0;
        }
        if ($result['content']) {
//            $result['content'] = htmlspecialchars_decode($result['content']);
            $result['summary'] = subtext(strip_tags($result['content']), 100);
            if ($result['cover']) {
                $id = explode(',', $result['cover'])[0];
                $result['img'] = D('Common/File')->find($id)['download_url'];
            } else {
                $result['img'] = $this->get_article_img($result['content']);
            }
        }
        if ($result['author']) {
            $user = D('User')->field('nickname,avatar')->find($result['author']);
            $result['author_name'] = $user['nickname'];
            $result['author_avatar'] = $user['avatar_format'];
        }
        if ($result['category_id']) {
            $result['category_name'] = M('Category')->where(['id' => $result['category_id']])->getFieldByid($result['category_id'], 'title');
        }
    }

    protected function _before_delete($options)
    {
        parent::_before_delete($options);
        if ($options['where']) {
            $this->delete_list = $this->where($options['where'])->select();
        }
    }

    protected function _after_delete($data, $options)
    {
        parent::_after_delete($data, $options);
        if ($this->delete_list) {
            foreach ($this->delete_list as $row) {
                M('ArticleReply')->where(['post_id' => $row['id']])->delete();
            }
        }
    }

    public function get_article_img($content)
    {
        if (preg_match('!\<img.*?src\=[\'|"](.+?)[\'|"].*?\>!is', $content, $m)) {
            return $m[1];
        }
        $log = D('Common','Logic');
        return $log->get_proto_domain().$log->toAbs(ADDON_PATH.'/Public/img/no.jpg').'?'.rand(999,9999);
    }
}